package org.example;

/**
 * 2. 两数相加
 * 给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。
 *
 * 请你将两个数相加，并以相同形式返回一个表示和的链表。
 *
 * 你可以假设除了数字 0 之外，这两个数都不会以 0 开头。
 *
 *  
 *
 * 示例 1：
 *
 *
 * 输入：l1 = [2,4,3], l2 = [5,6,4]
 * 输出：[7,0,8]
 * 解释：342 + 465 = 807.
 * 示例 2：
 *
 * 输入：l1 = [0], l2 = [0]
 * 输出：[0]
 * 示例 3：
 *
 * 输入：l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
 * 输出：[8,9,9,9,0,0,0,1]
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/add-two-numbers
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class AddTwoNumbersSolution {
    public ListNode addTwoNumbers(ListNode node1, ListNode node2) {
        ListNode node = null;
        ListNode one = node1;
        ListNode two = node2;
        int additionValue = 0;
        do {
            int value1 = 0;
            if (one != null){
                value1 = one.getVal();
            }
            int value2 = 0;
            if (two != null) {
                value2 = two.getVal();
            }
            int value = value1 + value2 + additionValue;
            additionValue = value / 10;
            ListNode curNode = new ListNode(value % 10);
            if (node == null) {
                node = curNode;
            } else {
                setLastNext(node, curNode);
            }
            one = one == null ? null : one.getNext();
            two = two == null ? null : two.getNext();
        } while (!(one == null && two == null));

        if (additionValue != 0) {
            ListNode curNode = new ListNode(additionValue);
            setLastNext(node, curNode);
        }
        return node;
    }

    public void setLastNext(ListNode node, ListNode curNode) {
        if (node.next == null) {
            node.next = curNode;
            return;
        }

        ListNode cur = node.next;
        while (true) {
            if (cur.next == null) {
                cur.next = curNode;
                return;
            }
            cur = cur.next;
        }
    }


    /** 以上为算法部分 */


    public static class ListNode {

        int val;

        ListNode next;

        ListNode() { }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public ListNode getNext() {
            return next;
        }

        public void setNext(ListNode next) {
            this.next = next;
        }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }

    public static ListNode generateListNode(int[] nums) {
        ListNode next = null;
        for (int index = nums.length -1; index > -1; index --) {
            next = new ListNode(nums[index], next);
        }
        return next;
    }

    public static void main(String[] args) {
        ListNode node1 = generateListNode(new int[]{9,9});
        ListNode node2 = generateListNode(new int[]{9});
        AddTwoNumbersSolution solution = new AddTwoNumbersSolution();
        System.out.println(solution.addTwoNumbers(node1, node2));
    }
}
